package com.biejiaogroup.librarymanager.controller;

import com.biejiaogroup.librarymanager.entity.Book;
import com.biejiaogroup.librarymanager.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: JesseChan
 * @Date: 2022/5/5
 */
@WebServlet("/updateBook")
public class UpdateServlet extends HttpServlet {
    private BookService updateService = new BookService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String bookID = request.getParameter("bookID");
        String bookName = request.getParameter("bookName");
        String bookAuthor = request.getParameter("bookAuthor");
        System.out.println("update "+bookID + " " + bookName + " " + bookAuthor);
        Book book = new Book();
        book.setBookID(Integer.parseInt(bookID));
        book.setBookName(bookName);
        book.setBookAuthor(bookAuthor);

        boolean res = updateService.updateBook(book);
        PrintWriter out = response.getWriter();
        if(res)
        {
            out.print("success");
        }
        else
        {
            out.print("failed");
        }

    }
}
